<?php

/**
 * 需要统计的广告标识
 * File Name：GuestAdModel.php
 * File Encoding：UTF-8
 * File New Time：2014-9-2 15:48:52
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class GuestAdlistModel extends BaseModel {

    public function tableName() {
        return '{{guest_adlist}}';
    }

    public function rules() {
        return array(
            array('admark', 'required', 'message' => '广告标识不能为空！'),
            array('admark', 'unique', 'message' => '该广告标识已经存在！'),
            array('adname', 'required', 'message' => '广告名称不能为空！'),
            array('adname', 'unique', 'message' => '该广告标识已经存在！'),
            array('alimama,createtime', 'safe'),
        );
    }

    //数据保存前操作
    protected function beforeSave() {
        if ($this->isNewRecord) {
            $this->createtime = time();
        }
        return true;
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回广告标识列表
     * @return type
     */
    public function getAdList() {
        $key = "GuestAdlistModel_getAdList";
        $cache = Yii::app()->cache->get($key);
        if (empty($cache)) {
            $data = $this->findAll();
            $cache = array();
            foreach ($data as $rs) {
                $cache[$rs['admark']] = $rs['adname'];
            }
            Yii::app()->cache->set($key, $cache, 3600);
        }
        return $cache;
    }

    /**
     * 根据广告标识返回名称
     * @param type $admark
     * @return type
     */
    public function getAdName($admark) {
        $adList = $this->getAdList();
        return $adList[$admark];
    }

    /**
     * 根据广告标识返回阿里妈妈标识
     * @param type $admark
     * @return type
     */
    public function getAlimama($admark) {
        $key = "GuestAdlistModel_getAlimama";
        $cache = Yii::app()->cache->get($key);
        if (empty($cache)) {
            $data = $this->findAll();
            $cache = array();
            foreach ($data as $rs) {
                $cache[$rs['admark']] = $rs['alimama'];
            }
            Yii::app()->cache->set($key, $cache, 3600);
        }
        return $cache[$admark];
    }

}
